package com.bootdo.clouddo.service.juejin.impl;

import java.util.ArrayList;
import java.util.List;

import com.bootdo.clouddo.utils.http.JuejinUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bootdo.clouddo.config.TaskConfig;
import com.bootdo.clouddo.dao.juejin.PostDao;
import com.bootdo.clouddo.domain.juejin.CategoryDO;
import com.bootdo.clouddo.domain.juejin.PostDO;
import com.bootdo.clouddo.domain.juejin.TagDO;
import com.bootdo.clouddo.service.juejin.IPostService;
import com.bootdo.clouddo.dto.ChannelEnum;
import com.bootdo.clouddo.utils.StringUtils;

@Service
public class PostServiceImpl implements IPostService {
	
	@Autowired
	private PostDao dao;

	@Override
	public int save(PostDO post) {
		return dao.insertSelective(post);
	}

	@Override
	public PostDO get(String id) {
		PostDO post = dao.selectByPrimaryKey(id);
		post.setUser(JuejinUtils.getNewImageHost(post.getUser()));
		return post;
	}

	@Override
	public List<PostDO> findTop(Integer num) {
		return dao.find("", "updatedAt", "desc", new RowBounds(0, num));
	}

	@Override
	public List<PostDO> find(String category[], String wd, String channel, Integer page, Integer size) {
		StringBuilder whereStr = new StringBuilder(" 1=1 ");
		if(StringUtils.isNotBlank(wd))
			whereStr.append("and title like '%" + wd + "%'");
		if(category != null && category.length >0)
			whereStr.append("and categoryId in ('" + StringUtils.join(category, "','") + "')");
		if(StringUtils.isNotBlank(channel) && StringUtils.equalsIgnoreCase(ChannelEnum.WEAPP.getValue(), channel))
			whereStr.append("and type = 'post'");
		List<PostDO> list = dao.find(whereStr.toString(), "updatedAt", "desc", new RowBounds(page, size));
		list.stream().forEach(post -> {
			post.setUser(JuejinUtils.getNewImageHost(post.getUser()));
		});
		return list;
	}

	@Override
	public List<CategoryDO> findCategoriesByUser(String userId) {
		List<String> categoriesStr = dao.findCategoriesByUser(userId);
		if(categoriesStr == null || categoriesStr.isEmpty())
			return null;
		List<CategoryDO> list = new ArrayList<CategoryDO>();
		for (String cat : categoriesStr) {
			if(StringUtils.isNotBlank(cat))
				list.add(JSON.toJavaObject(JSON.parseObject(cat), CategoryDO.class));
		}
		return list;
	}

	@Override
	public List<TagDO> findTagsByUser(String userId) {
		List<String> tagsStr = dao.findTagsByUser(userId);
		if(tagsStr == null || tagsStr.isEmpty())
			return null;
		List<TagDO> tags = new ArrayList<TagDO>();
		TagDO tag = null;
		for (String tagStr : tagsStr) {
			if(StringUtils.isNotBlank(tagStr)) {
				JSONArray ja = JSON.parseArray(tagStr);
				for (Object jo : ja) {
					tag = JSON.toJavaObject((JSON)jo, TagDO.class);
					if(!tags.contains(tag))
						tags.add(tag);
				}
			}
		}
		return tags;
	}

	@Override
	public List<PostDO> findByUser(String userId, Integer page, Integer size) {
		List<PostDO> list = dao.find("userId='" + userId + "'", "updatedAt", "desc", new RowBounds(page, size));
		list.stream().forEach(post -> {
			post.setUser(JuejinUtils.getNewImageHost(post.getUser()));
		});
		return list;
	}

	@Override
	public boolean exists(String id) {
		if(TaskConfig.juejinIds.contains(id)) {
			return true;
		}
		boolean e = dao.existsWithPrimaryKey(id);
		if(e)
			TaskConfig.juejinIds.add(id);
		return e;
	}
	
}
